﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO;
using System.Text;
using System.Windows.Forms;

using ScintillaNet;
using Descriptions;
using LPMEDEditor;

namespace IHMainApp
{
    public class LPMEDFactory : IDocumentsFactory
    {
        List<LPMEDDocument> documents_in_project = new List<LPMEDDocument>();
        MenuControl menuctrl;

        public LPMEDFactory()
        {
             
        }

        #region IDocumentsFactory Members

        public List<ICompilationUnit> GetCompilationUnits()
        {
            List<CompilationUnit> units=new List<CompilationUnit>();
            foreach (var lpmed in documents_in_project)
            {
                if (lpmed.IsCompile)
                    units.Add(new CompilationUnit { LPMED = lpmed.Document.Text });
            }
            return units.ConvertAll < ICompilationUnit>(val => val as ICompilationUnit) ;
        }

        public void Initialize(DomainModel model, Core.Logger.ILogger logger)
        {
            
        }

        public ICodeEditor LoadExisting(string Path)
        {
            LPMEDDocument doc = new LPMEDDocument(Path);
            documents_in_project.Add(doc);

            doc.Document.ConfigurationManager.Language = "lpmed";
            doc.Text = doc.Name;

            byte[] data = null;
            using (FileStream fs = File.Open(Path, FileMode.Open))
            {
                data = new byte[fs.Length];
                fs.Read(data, 0, (int)fs.Length);
            }

            doc.Document.RawText = data;
            
            return doc;
        }

        public ICodeEditor New(string path, Device device)
        {
            LPMEDDocument doc = new LPMEDDocument(path);
            documents_in_project.Add(doc);

            doc.Text = doc.Name;
            doc.Document.ConfigurationManager.Language = "lpmed";
            return doc;
        }

        public void RemoveFromProject(ICodeEditor doc)
        {
            if(doc is ICodeEditor)
            documents_in_project.Remove((LPMEDDocument)doc);
        }

        public List<ToolStripMenuItem> GetPluginMenuItems()
        {
            if(menuctrl==null)
                menuctrl = new MenuControl(this);

            List<ToolStripMenuItem> menu = new List<ToolStripMenuItem>();
            menu.Add(menuctrl.editToolStripMenu);
            menu.Add(menuctrl.viewStripMenuItem);
            return menu;
        }

        #endregion

        public LPMEDDocument ActiveDocument
        {
            get
            {
                foreach (var lpmed in documents_in_project)
                    if (lpmed.Visible)
                        return lpmed;
                return null;
            }
        }

        #region frmDocEvents

        /*internal void printToolStripMenuItem_Click(object sender, EventArgs e)
        {
            ActiveDocument.Document.Printing.Print();
        }

        internal void printPreviewToolStripMenuItem_Click(object sender, EventArgs e)
        {
            ActiveDocument.Document.Printing.PrintPreview();
        }*/

        internal void undoToolStripMenuItem_Click(object sender, EventArgs e)
        {
            ActiveDocument.Document.UndoRedo.Undo();
        }

        internal void redoToolStripMenuItem_Click(object sender, EventArgs e)
        {
            ActiveDocument.Document.UndoRedo.Redo();
        }

        internal void cutToolStripMenuItem_Click(object sender, EventArgs e)
        {
            ActiveDocument.Document.Clipboard.Cut();
        }

        internal void copyToolStripMenuItem_Click(object sender, EventArgs e)
        {
            ActiveDocument.Document.Clipboard.Copy();
        }

        internal void pasteToolStripMenuItem_Click(object sender, EventArgs e)
        {
            ActiveDocument.Document.Clipboard.Paste();
        }

        internal void selectAllToolStripMenuItem_Click(object sender, EventArgs e)
        {
            ActiveDocument.Document.Selection.SelectAll();
        }

        internal void findToolStripMenuItem_Click(object sender, EventArgs e)
        {
            ActiveDocument.Document.FindReplace.ShowFind();
        }

        internal void replaceToolStripMenuItem_Click(object sender, EventArgs e)
        {
            ActiveDocument.Document.FindReplace.ShowReplace();
        }

        internal void findInFilesToolStripMenuItem_Click(object sender, EventArgs e)
        {
            //	Coming someday...
        }

        internal void replaceInFilesToolStripMenuItem_Click(object sender, EventArgs e)
        {
            //	Coming someday...
        }

        internal void goToToolStripMenuItem_Click(object sender, EventArgs e)
        {
            ActiveDocument.Document.GoTo.ShowGoToDialog();
        }

        internal void toggleBookmarkToolStripMenuItem_Click(object sender, EventArgs e)
        {
            ActiveDocument.Document.Lines.Current.AddMarker(0);
        }

        internal void previosBookmarkToolStripMenuItem_Click(object sender, EventArgs e)
        {
            //	 I've got to redo this whole FindNextMarker/FindPreviousMarker Scheme
            Line l = ActiveDocument.Document.Lines.Current.FindPreviousMarker(1);
            if (l != null)
                l.Goto();
        }

        internal void nextBookmarkToolStripMenuItem_Click(object sender, EventArgs e)
        {
            //	 I've got to redo this whole FindNextMarker/FindPreviousMarker Scheme
            Line l = ActiveDocument.Document.Lines.Current.FindNextMarker(1);
            if (l != null)
                l.Goto();
        }

        internal void clearBookmarsToolStripMenuItem_Click(object sender, EventArgs e)
        {
            ActiveDocument.Document.Markers.DeleteAll(0);
        }

        internal void dropToolStripMenuItem_Click(object sender, EventArgs e)
        {
            ActiveDocument.Document.DropMarkers.Drop();
        }

        internal void collectToolStripMenuItem_Click(object sender, EventArgs e)
        {
            ActiveDocument.Document.DropMarkers.Collect();
        }

        internal void makeUpperCaseToolStripMenuItem_Click(object sender, EventArgs e)
        {
            ActiveDocument.Document.Commands.Execute(BindableCommand.UpperCase);
        }

        internal void makeLowerCaseToolStripMenuItem_Click(object sender, EventArgs e)
        {
            ActiveDocument.Document.Commands.Execute(BindableCommand.LowerCase);
        }

        internal void commentStreamToolStripMenuItem_Click(object sender, EventArgs e)
        {
            ActiveDocument.Document.Commands.Execute(BindableCommand.StreamComment);
        }

        internal void commentLineToolStripMenuItem_Click(object sender, EventArgs e)
        {
            ActiveDocument.Document.Commands.Execute(BindableCommand.LineComment);
        }

        internal void uncommentLineToolStripMenuItem_Click(object sender, EventArgs e)
        {
            ActiveDocument.Document.Commands.Execute(BindableCommand.LineUncomment);
        }

        internal void autocompleteToolStripMenuItem_Click(object sender, EventArgs e)
        {
            ActiveDocument.Document.AutoComplete.Show();
        }

        internal void insertSnippetToolStripMenuItem_Click(object sender, EventArgs e)
        {
            ActiveDocument.Document.Snippets.ShowSnippetList();
        }

        internal void surroundWithToolStripMenuItem_Click(object sender, EventArgs e)
        {
            ActiveDocument.Document.Snippets.ShowSurroundWithList();
        }

        internal void whiteSpaceToolStripMenuItem_Click(object sender, EventArgs e)
        {
     /*       whiteSpaceToolStripMenuItem.Checked = !whiteSpaceToolStripMenuItem.Checked;

            if (whiteSpaceToolStripMenuItem.Checked)
            {
                ActiveDocument.Document.WhiteSpace.Mode = WhiteSpaceMode.VisibleAlways;
            }
            else
            {
                ActiveDocument.Document.WhiteSpace.Mode = WhiteSpaceMode.Invisible;
            }
      * */
        }

        internal void wordWrapToolStripMenuItem_Click(object sender, EventArgs e)
        {
            /*
            wordWrapToolStripMenuItem.Checked = !wordWrapToolStripMenuItem.Checked;

            if (wordWrapToolStripMenuItem.Checked)
            {
                ActiveDocument.Document.LineWrap.Mode = WrapMode.Word;
            }
            else
            {
                ActiveDocument.Document.LineWrap.Mode = WrapMode.None;
            }
             */
        }

        internal void endOfLineToolStripMenuItem_Click(object sender, EventArgs e)
        {
            /*
            endOfLineToolStripMenuItem.Checked = !endOfLineToolStripMenuItem.Checked;
            ActiveDocument.Document.EndOfLine.IsVisible = endOfLineToolStripMenuItem.Checked;
             */ 
        }

        internal void zoomInToolStripMenuItem_Click(object sender, EventArgs e)
        {
            ActiveDocument.Document.Zoom++;
        }

        internal void zoomOutToolStripMenuItem_Click(object sender, EventArgs e)
        {
            ActiveDocument.Document.Zoom--;
        }

        internal void resetZoomToolStripMenuItem_Click(object sender, EventArgs e)
        {
            ActiveDocument.Document.Zoom = 0;
        }

        internal void lineNumbersToolStripMenuItem_Click(object sender, EventArgs e)
        {
            /*lineNumbersToolStripMenuItem.Checked = !lineNumbersToolStripMenuItem.Checked;
            if (lineNumbersToolStripMenuItem.Checked)
                ActiveDocument.Document.Margins.Margin0.Width = 35;
            else
                ActiveDocument.Document.Margins.Margin0.Width = 0;
             */ 
        }

        internal void foldLevelToolStripMenuItem_Click(object sender, EventArgs e)
        {

            ActiveDocument.Document.Lines.Current.FoldExpanded = true;
        }

        internal void unfoldLevelToolStripMenuItem_Click(object sender, EventArgs e)
        {

            ActiveDocument.Document.Lines.Current.FoldExpanded = false;
        }

        internal void foldAllToolStripMenuItem_Click(object sender, EventArgs e)
        {
            foreach (Line l in ActiveDocument.Document.Lines)
            {
                l.FoldExpanded = true;
            }
        }

        internal void unfoldAllToolStripMenuItem_Click(object sender, EventArgs e)
        {
            foreach (Line l in ActiveDocument.Document.Lines)
            {
                l.FoldExpanded = false;
            }
        }

        internal void navigateForwardToolStripMenuItem_Click(object sender, EventArgs e)
        {
            ActiveDocument.Document.DocumentNavigation.NavigateForward();
        }

        internal void navigateBackwardToolStripMenuItem_Click(object sender, EventArgs e)
        {
            ActiveDocument.Document.DocumentNavigation.NavigateBackward();
        }
        #endregion
    }

    internal class CompilationUnit : ICompilationUnit
    {
        public string LPMED { get; set; }
        #region ICompilationUnit Members

        public string Compile(out ErrorReport ErReport)
        {
            
            ErReport = new ErrorReport();
            return LPMED;
        }

        #endregion
    }

}